package com.szcinda.service.dto.analysis;

import com.szcinda.repository.OrderFee;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class GhOrderAnalysisDto implements Serializable {
    public String id;
    private String date;
    public String clientName;
    public String carrierName;
    public String organizationName;
    public List<FeeDto> feeList = new ArrayList<>();

    public List<FeeDto> outFeeList = new ArrayList<>();

    public void addFee(OrderFee orderFee, String clientName) {
        FeeDto existFee = feeList.stream().filter(fee -> clientName.equals(fee.getClientName()) && (StringUtils.isEmpty(orderFee.getCurrency())
                || fee.getCurrency().equals(orderFee.getCurrency())))
                .findFirst().orElse(null);
        if (existFee != null) {
            if(StringUtils.isEmpty(existFee.getCurrency())){
                return;
            }
            existFee.setFeeCount(existFee.getFeeCount() + orderFee.getFeeCount());
        } else {
            FeeDto feeDto = FeeDto.convertFeeDto(orderFee);
            if(StringUtils.isEmpty(feeDto.getCurrency())){
                return;
            }
            feeDto.setClientName(clientName);
            feeList.add(feeDto);
        }
    }

    public void addOutFee(OrderFee orderFee, String carrierName) {
        FeeDto existFee = outFeeList.stream().filter(fee -> carrierName.equals(fee.getCarrierName()) && (StringUtils.isEmpty(orderFee.getCurrency())
                || fee.getCurrency().equals(orderFee.getCurrency())))
                .findFirst().orElse(null);
        if (existFee != null) {
            if(StringUtils.isEmpty(existFee.getCurrency())){
                return;
            }
            existFee.setFeeCount(existFee.getFeeCount() + orderFee.getFeeCount());
        } else {
            FeeDto feeDto = FeeDto.convertFeeDto(orderFee);
            if(StringUtils.isEmpty(feeDto.getCurrency())){
                return;
            }
            feeDto.setCarrierName(carrierName);
            outFeeList.add(feeDto);
        }
    }

    @Data
    public static class FeeDto implements Serializable {
        public String clientName;
        public String carrierName;
        public double feeCount;
        public String currency;

        public static FeeDto convertFeeDto(OrderFee orderFee) {
            FeeDto feeDto = new FeeDto();
            feeDto.setCurrency(orderFee.getCurrency());
            feeDto.setFeeCount(orderFee.getFeeCount());
            return feeDto;
        }
    }
}
